import { getMemberAdd, saveWeixinAdd, addDefault, apiAddressDelete } from '../../api/index'
import { getRoute } from '../../common/js/mixin'
let page = {
  /**
   * 页面的初始数据
   */
  data: {
    showBg: false,
    hasAdd: false,
    address: {},
    addList: [],
    loads: true
  },
  closeBg () {
    this.setData({
      showBg: false
    })
  },
  selectAdd (e) {
    if (this.orderName) {
      const router = this.getRoute()
      router.changeAdd(e.currentTarget.dataset.item)
      wx.navigateBack({
        delta: 1
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options)
    this.orderName = options.name
    this.setData({
      isIphoneX: getApp().globalData.isIphoneX
    })
  },
  // 用户地址管理
  getAddList () {
    getMemberAdd().then(
      res => {
        this.setData({
          addList: res.data.data,
          loads: false
        })
        wx.stopPullDownRefresh()
      }
    )
  },
  // 设为默认地址
  selectDef (e) {
    addDefault({ id: e.currentTarget.dataset.id }).then(
      res => {
        this.getAddList()
      }
    )
  },
  // 新增地址
  addNew () {
    this.setData({
      showBg: true
    })
  },
  // 编辑地址
  editAdd (e) {
    let editId = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: `../edit-address/edit-address?data=${editId}`
    })
  },
  // 删除地址
  cancelAdd (e) {
    let addId = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '您确定要删除所选地址吗？',
      success: (res) => {
        if (res.confirm) {
          apiAddressDelete({ token: wx.getStorageSync('token'), id: addId }).then(
            res => {
              wx.showToast({
                title: '删除成功',
                icon: 'none'
              })
              setTimeout(() => {
                this.getAddList()
              }, 1200)
            }
          )
        } else if (res.cancel) {
        }
      }
    })
  },
  showBg () {
    this.setData({
      showBg: true
    })
  },
  addAdress () {
    this.setData({
      showBg: false
    })
    wx.navigateTo({
      url: '../edit-address/edit-address'
    })
  },
  // 选择微信地址
  addWx () {
    wx.chooseAddress({
      success: (res) => {
        this.setData({
          address: res,
          showBg: false
        })
        saveWeixinAdd({
          consigner: res.userName,
          mobile: res.telNumber,
          phone: '',
          province: res.provinceName,
          city: res.cityName,
          district: res.countyName,
          address: res.detailInfo,
          zip_code: res.nationalCode,
          alias: '',
          token: wx.getStorageSync('token')
        }).then(
          res1 => {
            this.getAddList()
            if (this.orderName) {
              const router = this.getRoute()
              router.changeAdd()
            }
          }
        )
      },
      fail: (res) => {
        wx.showToast({
          title: '授权失败',
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    this.getAddList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {
    this.setData({
      addList: []
    })
    this.getAddList()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  }
}
getRoute(page)
Page(page)
